package com.leetcode.LC;

import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;

public class LC1365 {
    public int[] smallerNumbersThanCurrent(int[] nums) {

        TreeSet<Integer> set = new TreeSet<>();
        for (int num : nums) {
            set.add(num);
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        int n = 0;
        for (Integer integer : set) {
            map.put(integer, n++);
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = map.get(nums[i]);
        }
        return nums;
    }

    public int[] smallerNumbersThanCurrent2(int[] nums) {

        int[] num2=nums.clone();
        Arrays.sort(nums);
        HashMap<Integer, Integer> map = new HashMap<>();
        int n = 0;
        for (int integer : nums) {
            if (!map.containsKey(integer)) {
                map.put(integer, n);
            }
            n++;
        }
        for (int i = 0; i < nums.length; i++) {
            num2[i] = map.get(num2[i]);
        }
        return num2;
    }

    public static void main(String[] args) {
        for (int i : new LC1365().smallerNumbersThanCurrent2(new int[]{8,1,2,2,3})) {
            System.out.print(i+",");
        }
    }
}
